package com.tiancheng.trade.order.service.impl;

import com.tiancheng.trade.order.bo.cmq.TallyFlowSubOrderMsgBO;
import com.tiancheng.trade.order.bo.cmq.TallyMsgBO;
import com.tiancheng.trade.order.bo.cmq.TallyOrderInfoBO;
import com.tiancheng.trade.order.bo.cmq.TallyOrderServiceSuccessBO;
import com.tiancheng.trade.order.bo.notify.PayResultOrderBO;
import com.tiancheng.trade.order.enums.OrderActionTypeEnum;
import com.tiancheng.trade.order.enums.RefundStatusEnum;
import com.tiancheng.trade.order.model.Order;
import com.tiancheng.trade.order.model.RefundOrdersDetail;
import com.tiancheng.trade.order.model.SubOrder;
import com.tiancheng.trade.order.service.OrderService;
import com.tiancheng.trade.order.service.RefundOrdersDetailService;
import com.tiancheng.trade.order.service.SubOrderService;
import com.tiancheng.trade.order.service.TallyService;
import com.tiancheng.trade.order.mq.MessageProducer;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 推送给对账中心的处理Service接口实现
 *
 * @author kellen
 * @date 2024/5/19
 */
@Service
public class TallyServiceImpl implements TallyService {

    @Resource
    private SubOrderService subOrderService;
    @Resource
    private RefundOrdersDetailService refundOrdersDetailService;
    @Resource
    private OrderService orderService;
    @Resource
    private MessageProducer messageProducer;

    @Override
    public boolean verification(String verificationId, Integer amount, LocalDateTime time, String subOrderId) {
        SubOrder subOrder = subOrderService.findBySubId(subOrderId);
        Order order = orderService.get(subOrder.getOrderId());

        List<TallyMsgBO> msgList = new ArrayList<>();

        TallyMsgBO msgBO = this.getMsg(order, subOrder);
        msgBO.setVerificationId(verificationId);
        msgBO.setVerifiedAmount(subOrder.getSubOrderAmount());
        msgBO.setVerifiedTime(time);
        msgBO.setCostPrice(subOrder.getProductCost());
        msgBO.setTradeType("VE");
        msgList.add(msgBO);

        // 核销时，如果子订单有退款记录，也一并给出
        if(RefundStatusEnum.success.code().equals(subOrder.getRefundStatus())) {
            List<RefundOrdersDetail> refundOrdersDetails = refundOrdersDetailService.findSuccessBySubId(subOrderId);
            refundOrdersDetails.forEach(refund -> {
                TallyMsgBO refundMsgBO = this.getMsg(order, subOrder);
                refundMsgBO.setRefundId(refund.getRefundId());
                refundMsgBO.setApplyRefundAmount(refund.getRefundApplyAmount());
                refundMsgBO.setRefundAmount(refund.getRefundAmount());
                refundMsgBO.setRefundTime(refund.getRefundTime());
                refundMsgBO.setTradeType("RE");
                msgList.add(refundMsgBO);
            });
        }
        return true;
    }

    @Override
    public boolean refund(RefundOrdersDetail refundOrdersDetail) {
        SubOrder subOrder = subOrderService.findBySubId(refundOrdersDetail.getSubOrderId());

        // 先推送子订单的数据
        TallyFlowSubOrderMsgBO subOrderMsgBO = new TallyFlowSubOrderMsgBO();
        subOrderMsgBO.setFlowId(refundOrdersDetail.getFlowId());
        subOrderMsgBO.setPaymentId(refundOrdersDetail.getPaymentId());
        subOrderMsgBO.setOrderId(refundOrdersDetail.getOrderId());
        subOrderMsgBO.setSubOrderId(refundOrdersDetail.getSubOrderId());
        subOrderMsgBO.setOrderSource(refundOrdersDetail.getOrderSource());
        subOrderMsgBO.setMerchantCode(refundOrdersDetail.getMerchantCode());
        subOrderMsgBO.setMerchantName(refundOrdersDetail.getMerchantName());
        subOrderMsgBO.setProductCode(refundOrdersDetail.getProductCode());
        subOrderMsgBO.setProductName(refundOrdersDetail.getProductName());
        subOrderMsgBO.setProductDetail(subOrder.getProductDetail());
        subOrderMsgBO.setProductCount(subOrder.getProductCount());
        subOrderMsgBO.setSubOrderAmount(subOrder.getSubOrderAmount());
        subOrderMsgBO.setAmount(subOrder.getPayAmount());
        subOrderMsgBO.setRefundAmount(subOrder.getRefundAmount());
        subOrderMsgBO.setTransactionType("refund");

        // 如果子订单未核销，不需要推送
        //if(subOrder.getVerificationTime() == null) return future;

        Order order = orderService.get(subOrder.getOrderId());

        List<TallyMsgBO> msgList = new ArrayList<>();

        TallyMsgBO msgBO = this.getMsg(order, subOrder);
        msgBO.setRefundId(refundOrdersDetail.getRefundId());
        msgBO.setApplyRefundAmount(refundOrdersDetail.getRefundApplyAmount());
        msgBO.setRefundAmount(refundOrdersDetail.getRefundAmount());
        msgBO.setRefundTime(refundOrdersDetail.getRefundTime());
        //msgBO.setVerifiedTime(subOrder.getVerificationTime());
        msgBO.setCostPrice(subOrder.getProductCost());
        msgBO.setTradeType("RE");
        msgList.add(msgBO);

        //return messageProducer.tally(msgList);
        return true;
    }

    @Override
    public boolean pay(PayResultOrderBO order) {
        return true;
    }

    @Override
    public boolean orderActionSend(OrderActionTypeEnum actionTypeEnum, String orderId, List<String> subOrderIds,String flowId) {
        boolean result=true;
        try {
            TallyOrderInfoBO tallyOrderInfoBO = new TallyOrderInfoBO(orderId, subOrderIds, actionTypeEnum.getCode(),flowId);
            //messageProducer.tallyPayInfo(tallyOrderInfoBO);
        }catch (Exception e){
            result=false;
        }
        return result;
    }

    @Override
    public boolean orderServiceSuccess(TallyOrderServiceSuccessBO orderServiceSuccessBO) {
        try {
            //messageProducer.tallyOrderServiceSuccess(orderServiceSuccessBO);
            return true;
        }catch (Exception e){
            return false;
        }
    }

    /**
     * 构造记录消息对象
     * @param order
     * @param subOrder
     * @return
     */
    private TallyMsgBO getMsg(Order order, SubOrder subOrder) {
        TallyMsgBO tallyMsgBO = new TallyMsgBO();
        tallyMsgBO.setPayTime(order.getPayTime());
        tallyMsgBO.setFlowId(order.getFlowId());
        tallyMsgBO.setOrderId(order.getOrderId());
        tallyMsgBO.setSubOrderId(subOrder.getSubOrderId());
        tallyMsgBO.setSubOrderAmount(subOrder.getSubOrderAmount());
        tallyMsgBO.setSubOrderCount(order.getSubOrderCnt());
        tallyMsgBO.setDiscountAmount(subOrder.getDiscount());
        tallyMsgBO.setMerchantCode(subOrder.getMerchantCode());
        tallyMsgBO.setMerchantName(subOrder.getMerchantName());
        tallyMsgBO.setProductCode(subOrder.getProductCode());
        tallyMsgBO.setProductName(subOrder.getProductName());
        tallyMsgBO.setPayChannelCode(order.getPayChannel());
        tallyMsgBO.setOrderSource(order.getOrderSource());
        tallyMsgBO.setProductCount(subOrder.getProductCount());
        tallyMsgBO.setActualAmount(subOrder.getPayAmount());
        return tallyMsgBO;
    }
}
